package dbx

import (
	"time"

	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
)

type InfluxDB struct {
	BatchSize          uint          `json:"batch_size" toml:"batch_size" yaml:"batch_size"`
	FlushInterval      uint          `json:"flush_interval" toml:"flush_interval" yaml:"flush_interval"`
	RetryInterval      uint          `json:"retry_interval" toml:"retry_interval" yaml:"retry_interval"`
	MaxRetries         uint          `json:"max_retries" toml:"max_retries" yaml:"max_retries"`
	RetryBufferLimit   uint          `json:"retry_buffer_limit" toml:"retry_buffer_limit" yaml:"retry_buffer_limit"`
	MaxRetryInterval   uint          `json:"max_retry_interval" toml:"max_retry_interval" yaml:"max_retry_interval"`
	MaxRetryTime       uint          `json:"max_retry_time" toml:"max_retry_time" yaml:"max_retry_time"`
	ExponentialBase    uint          `json:"exponential_base" toml:"exponential_base" yaml:"exponential_base"`
	LogLevel           uint          `json:"log_level" toml:"log_level" yaml:"log_level"`
	Precision          time.Duration `json:"precision" toml:"precision" yaml:"precision"`
	UseGZip            bool          `json:"use_gzip" toml:"use_gzip" yaml:"use_gzip"`
	ApplicationName    string        `json:"application_name" toml:"application_name" yaml:"application_name"`
	HTTPRequestTimeout uint          `json:"http_request_timeout" toml:"http_request_timeout" yaml:"http_request_timeout"`

	TLS TLS `json:"tls,omitempty" toml:"tls,omitempty" yaml:"tls,omitempty"`
}

func (r InfluxDB) Option(handles ...func(options *influxdb2.Options)) *influxdb2.Options {
	opts := influxdb2.DefaultOptions()
	opts.SetBatchSize(r.BatchSize)
	opts.SetFlushInterval(r.FlushInterval)
	opts.SetRetryInterval(r.RetryInterval)
	opts.SetMaxRetries(r.MaxRetries)
	opts.SetRetryBufferLimit(r.RetryBufferLimit)
	opts.SetMaxRetryInterval(r.MaxRetryInterval)
	opts.SetMaxRetryTime(r.MaxRetryTime)
	opts.SetExponentialBase(r.ExponentialBase)
	opts.SetLogLevel(r.LogLevel)
	opts.SetPrecision(r.Precision)
	opts.SetUseGZip(r.UseGZip)
	opts.SetHTTPRequestTimeout(r.HTTPRequestTimeout)
	opts.SetApplicationName(r.ApplicationName)

	if r.TLS.IsEnable() {
		opts.SetTLSConfig(r.TLS.Tls())
	}

	for _, handle := range handles {
		handle(opts)
	}
	return opts
}
